#include <QApplication>
#include <QTranslator>
#include <QLibraryInfo>
#include <QTextCodec>
#include <QLocale>
#include <QDebug>
#include <QDir>
#include <QString>
#include <QSplashScreen>
#if (QT_VERSION >= 0x050000)
#include <QtWidgets>
#endif

#include <ctime>

#include "config.h"

#include "mainwindow.h"
#include <filename.h>

#include <QtGui>


int test ()
{
    return 0;
}
//---------------
void initDico ()
{
    return;
#if (0)
    QProcess process;
    process.execute ("aspell dump master fr | sort > /home/gilles/Bureau/dico_francais.txt");
    QFile file ("/home/gilles/Bureau/dico_francais.txt");
    file.open (QIODevice::ReadOnly);
    QString contain (file.readAll ());
    file.close ();
    QStringList list (contain.split ('\n', QString::SkipEmptyParts));
    QStringList result;
    foreach (const QString &word, list) {
        QString wd = word;
        int pos = word.indexOf ('/');
        if (pos >= 0) {
            wd.truncate (pos);
        }
        if (wd.length () > 1)
            result << wd;
    }
    QByteArray data (result.join ("\n").toLatin1 ());
    QFile file2 ("/home/gilles/Bureau/dic_fr.txt");
    file2.open (QIODevice::WriteOnly);
    file2.write (data);
    file2.close ();
#endif
}
//---------------
bool initDirs ()
{
    static QString error (QObject::tr ("Impossible de créer le répertoire %1\nArrêt de l'application."));
    bool ok = true;
    const QDir dir;

    if (! dir.mkpath (Config::appWorkPath ())) {
        qDebug () << error.arg (Config::appWorkPath ());
        ok = false;
    }

    if (! dir.mkpath (Config::appHomePath ())) {
        qDebug () << error.arg (Config::appHomePath ());
        ok = false;
    }

    if (! dir.mkpath (Config::appExportPath ())) {
        qDebug () << error.arg (Config::appExportPath ());
        ok = false;
    }
    return ok;
}
//--------------------------------
int main (int argc, char *argv[])
{
    //	****************************
    //	* Initialisations importantes *
    //	****************************

    qsrand(time(NULL));	// semence aléatoire

    QApplication::setDesktopSettingsAware (true);
    QApplication::setQuitOnLastWindowClosed (true);

#if (QT_VERSION < 0x050000)
    QApplication::setGraphicsSystem ("raster");
#endif

    QApplication app (argc, argv);

    app.setOrganizationName ("PapaJaac");
    app.setApplicationName (Config::appName ());
    app.setApplicationVersion (Config::appVersion ());
    app.setWindowIcon (Config::appIcon ());
    test ();

    QSplashScreen splash (QPixmap (":/images/splash.png"));
    splash.show ();
    app.processEvents();

#if (QT_VERSION < 0x050000)
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
#endif
    QTextCodec::setCodecForLocale (QTextCodec::codecForName ("UTF-8"));

    // pour avoir les boutons des boîtes de dialogue dans la langue locale (fr par défaut)
    QTranslator translator;
    translator.load (
                "qt_" + QLocale::system ().name (),
                QLibraryInfo::location (QLibraryInfo::TranslationsPath)
                );
    app.installTranslator (&translator);

    // pour avoir l'appli localisée
    QTranslator appTranslator;
    appTranslator.load (
                Config::appName () + '_' + QLocale::system ().name (),
                ":/translate/"
                );
    app.installTranslator (&appTranslator);

    splash.showMessage (QObject::tr ("%1 version %2 :\nLa meilleure interface graphique\npour le moteur de ROC Tesseract\n\n")
                        .arg (Config::appName ())
                        .arg (Config::appVersion ()),
                        Qt::AlignBottom | Qt::AlignHCenter,
                        Qt::black);
    app.processEvents();

    if (!initDirs ()) return 99;

    initDico ();
    QStringList args (QApplication::arguments ());
    QString documentPath;

    if (args.size () > 1) {
        for (int index = 1; index != args.size (); index++) {
            const QString& arg (args.at (index));
            if (QFile (arg).exists ()) {
                documentPath = arg;
                break;
            }
        }
    }
    MainWindow* w = new MainWindow (documentPath);
    splash.finish (w);
    w->show ();

    int ret = app.exec ();

    delete w;
    return ret;
}
